﻿using BanquetPortion.Database;
using BanquetPortion.Models;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Internal;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Threading.Tasks;

namespace BanquetPortion.Services
{
    public class DataDictionaryRepository : IDataDictionaryRepository
    {
        private readonly AppDbContext _context;
        public DataDictionaryRepository(AppDbContext context)
        {
            _context = context;
        }
        public async Task<IEnumerable<DataDictionary>> GetDataDictionarysAsync()
        {
            return await _context.DataDictionarys.ToArrayAsync();
        }
        public async Task<DataDictionary> GetDataDictionaryByIdAsync(int dataDictionaryId)
        {
            return await _context.DataDictionarys.FirstOrDefaultAsync(d => d.Id == dataDictionaryId);
        }
        public void CreateDataDictionary(DataDictionary dataDictionary)
        {
            if (dataDictionary == null)
            {
                // 空数据异常
                throw new ArgumentNullException(nameof(dataDictionary));
            }
            _context.DataDictionarys.Add(dataDictionary);
        }
        public void DeleteDataDictionary(DataDictionary dataDictionary)
        {
            _context.DataDictionarys.Remove(dataDictionary);
        }
        public async Task<bool> DataDictionaryExistsAsync(int dataDictionaryId)
        {
            return await _context.DataDictionarys.AnyAsync(d => d.Id == dataDictionaryId);
        }
        public async Task<bool> SaveAsync()
        {
            return (await _context.SaveChangesAsync() >= 0);
        }
    }
}
